package parkshop.pmcourse.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import parkshop.pmcourse.domain.Product;
import parkshop.pmcourse.domain.Shop;
import parkshop.pmcourse.dto.PageBean;
import parkshop.pmcourse.dto.User;
import parkshop.pmcourse.service.ProductService;
import parkshop.pmcourse.service.SearchService;
import parkshop.pmcourse.service.ShopOwnerService;
import parkshop.pmcourse.service.ShopService;
import parkshop.pmcourse.utils.ConstantUtil;

/*
 * 卖家访问控制类
 */
@Controller
@RequestMapping("/shopOwner")
public class ShopOwnerCtrl {
	
	@Autowired
	private ShopOwnerService shopOwnerService;
	
	@Autowired
	private ProductService productService;
	
	@Autowired
	private ShopService shopService;
	@Autowired
	private SearchService searchService;
	/*
	 * Manage Products
	 * Add /Modify / Delete Products ( upload Photo’s Product)
	 */
	@GetMapping("/products/list/{pageNumber}")
	public String listproducts(@PathVariable int pageNumber,HttpServletRequest request, Model model){
		User curUser=(User) request.getSession().getAttribute(ConstantUtil.nameOfCurUser);
		Shop shop=shopOwnerService.getMyShop(curUser.getId());
		PageBean pageBean = productService.getProductsByPage(pageNumber,shop.getId());
		model.addAttribute("pageBean", pageBean);
		return "shopOwner/productManage";
	}
	@PostMapping("/products/search")
	public String searchProducts(String name,int pageNumber,HttpServletRequest request, Model model){
		User curUser=(User) request.getSession().getAttribute(ConstantUtil.nameOfCurUser);
		Shop shop=shopOwnerService.getMyShop(curUser.getId());
		//PageBean pageBean = productService.getProductsByPage(pageNumber,shop.getId());
		
		PageBean pageBean=searchService.searchPageProductByName(name, pageNumber);
		model.addAttribute("pageBean", pageBean);
		return "shopOwner/productManage";
	}
	@GetMapping("/product/add")
	public String goAddProduct(HttpServletRequest request, Model model) {
		
		return "shopOwner/productUpload";
	}
	@PostMapping("/product/add")
	public String doAddProduct(Product product,HttpServletRequest request, Model model, HttpSession httpSession) {
		User customer = (User) httpSession.getAttribute(ConstantUtil.nameOfCurUser);
		Shop shop = shopService.getShopByShopOwnId(customer.getId());
		product.setShopId(shop.getId());
		productService.saveProduct(product);
		return "redirect:/shopOwner/products/list/1";
	}
	
	
	@PostMapping("/product/modify")
	public String modifyProduct(Product product,HttpServletRequest request, Model model) {
		productService.updateProduct(product);
		return "redirect:/shopOwner/products/list/1";
	}
	@GetMapping("/product/modify/{productId}")
	public String goModifyProduct(@PathVariable Long productId,HttpServletRequest request, Model model) {
		Product product=productService.getProductById(productId);
		model.addAttribute("product", product);
		model.addAttribute("operate", "productUpdate");
		return "shopOwner/productUpload";
	}
	@GetMapping("/product/delete/{productId}")
	public String deleteProduct(@PathVariable int productId,HttpServletRequest request, Model model) {
		
		productService.deleteProductById(productId);
		
		return "redirect:/shopOwner/products/list/1";
	}
	/*
	 * Manage Advertisement
	Apply Advertisement
	Homepage Product list 
	 */
	// apply advertisement to seller own shop
	@PostMapping("/advertisement/apply")
	public String applyAdvertisement(HttpServletRequest request, Model model) {
		return null;
	}
	
	/*
	 * Add/Modify Shop Details
	Shop contacts / Email / Telephone
	 */
	@GetMapping("/shop/update")
	public String updateShopBasicInfo(Shop shop,HttpServletRequest request, Model model){
		
		
		return "shopOwner/manageShop";
	}

	
	/*
	 * update person info
	 */
	@GetMapping("/personInfo")
	public String goPersonInfo(HttpServletRequest request, Model model){
		
		return "module/personInfo";        	
	}
	@PostMapping("/personInfo/update")
	public String updatePersonInfo(User user,HttpServletRequest request, Model model){
		User curUser=(User) request.getSession(false).getAttribute(ConstantUtil.nameOfCurUser);
		
		if(user.getName()!=null&&!"".equals(user.getName().trim()))
			curUser.setName(user.getName());
		if(user.getPhone()!=null&&!"".equals(user.getPhone().trim()))
			curUser.setPhone(user.getPhone());
		shopOwnerService.updatePersonInfo(curUser);
		return "module/personInfo";        	
	}
	@PostMapping("/password/update")
	public String updatePassword(String oldPassword,String newPassword,HttpServletRequest request, Model model){
		User curUser=(User) request.getSession(false).getAttribute(ConstantUtil.nameOfCurUser);
		String message="update password failed , because your old password was not right";
		if(curUser.getPassword().equals(oldPassword)){
			curUser.setPassword(newPassword);
			shopOwnerService.updatePersonInfo(curUser);
			message="update password successful";
		}
		model.addAttribute("message", message);
		return "module/personInfo";        	
	}
	/*
	 * manage my shop
	 */
	@GetMapping("/shop/manage")
	public String manageShop(HttpServletRequest request, Model model){
		
		User curUser=(User) request.getSession(false).getAttribute(ConstantUtil.nameOfCurUser);
		Shop shop=shopOwnerService.getMyShop(curUser.getId());
		model.addAttribute("shop", shop);
		String mainPage="http://"+request.getServerName()+":"+request.getServerPort()+"/shop/"+shop.getId();
		model.addAttribute("mainPage", mainPage);
		return "shopOwner/shopManage";
	}
	@PostMapping("/shop/info/update")
	public String updateShopInfo(Shop shop,HttpServletRequest request, Model model){
		
		User curUser=(User) request.getSession(false).getAttribute(ConstantUtil.nameOfCurUser);
		Shop oldShop=shopOwnerService.getMyShop(curUser.getId());
		if(shop.getName()!=null&&!"".equals(shop.getName().trim()))
			oldShop.setName(shop.getName());
		if(shop.getEmail()!=null&&!"".equals(shop.getEmail().trim()))
			oldShop.setEmail(shop.getEmail());
		if(shop.getPhone()!=null&&!"".equals(shop.getPhone().trim()))
			oldShop.setPhone(shop.getPhone());
		if(shop.getContacts()!=null&&!"".equals(shop.getContacts().trim()))
			oldShop.setContacts(shop.getContacts());
		
		shopOwnerService.updateShopInfo(oldShop);
		
		model.addAttribute("shop", oldShop);
		return "shopOwner/shopManage";
	}
	
	/*
	 * Manage Order
	Processing Order -> Preparing for Shipment->Shipped->Complete
	 */
	
	/*
	 * View Sales History (Daily /Weekly/ Monthly/ Yearly)- Search
	 */

	/*
	 * View Shop Income from Sale
	 * Daily /Weekly /Monthly /Yearly
	 */
	
	
	
}